home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5368 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  76 lines

  1. Path: wkaufman.us.oracle.com!wkaufman
  2. From: wkaufman@wkaufman.us.oracle.com (William Kaufman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Apology, was: Re: Limit on #bytes inside of struct?
  5. Date: 12 Feb 1996 16:43:54 GMT
  6. Organization: Oracle Corporation, Redwood Shores CA
  7. Message-ID: <4fnqoa$f7t@inet-nntp-gw-1.us.oracle.com>
  8. References: <311F15D8.78D1@zess.uni-siegen.de> <9602121217.AA07160@dxmint.cern.ch> <311F4F4D.7784@zess.uni-siegen.de>
  9. NNTP-Posting-Host: wkaufman.us.oracle.com
  10.  
  11. In article <311F4F4D.7784@zess.uni-siegen.de> Markus Becker <becker@zess.uni-siegen.de> writes:
  12. ] > o As steps #1 and #2 mentioned above will show you, an array decays into
  13. ] >   a pointer only when used in a value context.  And, of course, pointers
  14. ] >   and arrays are _not_ equivalent and cannot be used interchangeably.
  15. ] I had a sentence somewhere in the back of my mind "that arrays and pointers
  16. ] in C are equivalent and completely interchangeable". Could it be K&R?
  17.  
  18.     I think it was the comp.lang.c FAQ:
  19.  
  20. 6.2:    But I heard that char a[] was identical to char *a.
  21.  
  22. A:    Not at all.  (What you heard has to do with formal parameters to
  23.     functions; see question 6.4.)  Arrays are not pointers.  The
  24.     array declaration char a[6] requests that space for six
  25.     characters be set aside, to be known by the name "a."  That is,
  26.     there is a location named "a" at which six characters can sit.
  27.     The pointer declaration char *p, on the other hand, requests a
  28.     place which holds a pointer, to be known by the name "p."  This
  29.     pointer can point almost anywhere: to any char, or to any
  30.     contiguous array of chars, or nowhere (see also questions 5.1
  31.     and 1.30).
  32.  
  33.     As usual, a picture is worth a thousand words.  The declarations
  34.  
  35.         char a[] = "hello";
  36.         char *p = "world";
  37.  
  38.     would initialize data structures which could be represented like
  39.     this:
  40.            +---+---+---+---+---+---+
  41.         a: | h | e | l | l | o |\0 |
  42.            +---+---+---+---+---+---+
  43.            +-----+     +---+---+---+---+---+---+
  44.         p: |  *======> | w | o | r | l | d |\0 |
  45.            +-----+     +---+---+---+---+---+---+
  46.  
  47.     It is important to realize that a reference like *x*[3]
  48.     generates different code depending on whether *x* is an array or
  49.     a pointer.  Given the declarations above, when the compiler sees
  50.     the expression a[3], it emits code to start at the location "a,"
  51.     move three past it, and fetch the character there.  When it sees
  52.     the expression p[3], it emits code to start at the location "p,"
  53.     fetch the pointer value there, add three to the pointer, and
  54.     finally fetch the character pointed to.  In other words, a[3] is
  55.     three places past (the start of) the object *named* a, while
  56.     p[3] is three places past the object *pointed to* by p.  In the
  57.     example above, both a[3] and p[3] happen to be the character
  58.     'l', but the compiler gets there differently.
  59.  
  60.     References: K&R2 Sec. 5.5 p. 104; CT&P Sec. 4.5 pp. 64-5.
  61.  
  62.     Please don't post again until you've read the FAQ.  Thank you.
  63.  
  64.     The FAQ is posted at the beginning of each month (and, in abridged
  65. form, in the middle of the month) to comp.lang.c and comp.answers.  It's
  66. also available by anonymous FTP from pit-manager.mit.edu or rtfm.mit.edu
  67. (directory pub/usenet/news.answers/C-faq/ and pub/usenet/comp.lang.c/);
  68. and ftp.uu.net (directory usenet/news.answers/C-faq/).
  69.  
  70.                                            -- Bill K.
  71.  
  72. Bill Kaufman,          | "People look better under arrest,...it gives you
  73. wkaufman@us.oracle.com |  an aggressive fashion look."  -- John Waters
  74.